

#include "../Header/gethtml.h"

using namespace std;

GetHtml::GetHtml()
{
	process_html_obj = new ProcessHtml;
	str_process = string();
	str_replace = string();
	str_html = string();
	str_html_mod = string();
	str_dest = string();
	file_name = string();
	infile = ifstream();
	outfile = ofstream();
	line_number = 0;
	HTML_DIR_NAME_LEN = 17;
	HTML_NAME_POS = 9;
	ReadProcessedFile();
}

void GetHtml::openHtmlFile()
{
	if (file_name.empty()) {
		cout << "please input the html name: " << flush;
		cin >> file_name;
		ofstream out_file("processed_html_file", ofstream::app);
		out_file << file_name << endl;
		out_file.close();
	}
	infile.open(file_name.c_str());
	string::size_type tmp_pos = file_name.find(".");
	str_html_mod = file_name.substr(0, tmp_pos) + "_mod.html"; 
	outfile.open(str_html_mod.c_str(), ofstream::app);
}

void GetHtml::closeHtmlFile()
{
	infile.close();
	outfile.close();
}

void GetHtml::getHtmlMsg()
{
	openHtmlFile();
	if (!infile) {
		cout << "Error: no file to read" << endl;
		return;
	}
	while (getline(infile, str_process)) {
		++line_number;
		if (str_process.empty()) {
			makeHtmlFile();
			continue;
		}
		string::size_type tpp_pos = str_process.find("tppabs");
		if (tpp_pos != string::npos && tpp_pos <= 33) {
			string::size_type href_pos = str_process.find("a href");
			if (href_pos == string::npos) {
				cout << "Error: this string can`t contain a html address" << endl;
			}
			else {
				str_html = str_process.substr(href_pos + 8, tpp_pos - 10 - href_pos);
				if (!FindIfProcessed(str_html)) {
					processSubHtmlFile(str_html);
					RecordProcessedHtml(str_html);
				}
				str_replace = str_html.substr(0, HTML_DIR_NAME_LEN) + "\\" + str_html;
				str_process.replace(HTML_NAME_POS, str_html.size(), str_replace);
			}
		}
		makeHtmlFile();
	}
	closeHtmlFile();
}

void GetHtml::makeHtmlFile()
{
	outfile << str_process << endl;
}

void GetHtml::processSubHtmlFile(string & str_sub_html)
{
	process_html_obj->processHtmlFile(str_sub_html);
}

void GetHtml::ReadProcessedFile()
{
	ifstream in_file("processed_html_file");
	if (!in_file) {
		ofstream out_file("processed_html_file");
		out_file.close();
		in_file.close();
		cout << "init the file to record the precess: processed_html_file" << endl;
		return;
	}
	string str_tmp;
	//getline(infile, str_tmp);
	getline(in_file, file_name);
	cout << "Now process the html file: " << file_name << endl;
	while (getline(in_file, str_tmp)) {
		if (!str_tmp.empty())
			processed_html_file_vector.push_back(str_tmp);
	}
	in_file.close();
	return;
}

void GetHtml::RecordProcessedHtml(const string & str_html)
{
	if(str_html.empty())
		return;
	processed_html_file_vector.push_back(str_html);
	ofstream out_file("processed_html_file", ofstream::app);
	out_file << str_html << endl;
	out_file.close();
	return;
}

bool GetHtml::FindIfProcessed(const string & str_html)
{
	vector<string>::reverse_iterator iter = processed_html_file_vector.rbegin();
	for (; iter !=  processed_html_file_vector.rend(); ++iter) {
		if (*iter == str_html)
			return true;
	}
	return false;
}